import 'package:flutter/material.dart';

/// 可输入多字折行
/// [title]:标题
/// [hintText]:提示文字
/// [initialValue]:初始显示的文字
/// [maxLength]:最大的文字数量
/// [hasLine]：是否有下划线
/// [onChanged]:输入变化时的回调
/// [maxLines]:最大行数
class MultiLineInputWidget extends StatefulWidget {
  final String title;
  final String hintText;
  final String initialValue;
  final int maxLength;
  final bool hasLine;
  final ValueChanged<String> onChanged;
  final int maxLines;

  MultiLineInputWidget(
      {this.title = '',
      this.hintText = '请输入内容',
      this.initialValue,
      this.maxLength,
      this.hasLine = true,
      this.onChanged,
      this.maxLines,});

  @override
  State<StatefulWidget> createState() {
    return _MultiLineInputState();
  }
}

class _MultiLineInputState extends State<MultiLineInputWidget> {
  final textEditingController = TextEditingController();

  @override
  void initState() {
    textEditingController.text = widget.initialValue;
    textEditingController.addListener(() {
      widget.onChanged(textEditingController.text);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(left: 16, top: 15, right: 16, bottom: 15),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(widget.title,
                    style: TextStyle(color: Color(0xFF666666), fontSize: 14)),
                SizedBox(
                  width: 35,
                ),
                Expanded(
                  child: TextField(
                    controller: textEditingController,
                    decoration: InputDecoration(
                        contentPadding: EdgeInsets.all(0),
                        border: InputBorder.none,
                        hintText: widget.hintText,
                        counterText: ""),
                    textAlign: TextAlign.right,
                    maxLines: widget.maxLines,
                    maxLength: widget.maxLength,
                    style: TextStyle(fontSize: 14, color: Color(0xFF333333)),
                  ),
                ),
              ],
            ),
          ),
          Visibility(
              visible: widget.hasLine,
              child:
                  Divider(height: 0.5, indent: 16, color: Color(0xFFEBEBEB))),
        ],
      ),
    );
  }

  @override
  void dispose() {
    textEditingController.dispose();
    super.dispose();
  }
}
